﻿using Ducu.GLB.Global;
using Ducu.SRV.ServiceInteface;
using Ducu.SRV.ViewModel.Shared;
using Ducu.SRV.ViewModel.Task;
using Ducu.SRV.ViewModel.Task.Shared;
using Ducu.UI.Responsible.Filter;
using Global.Core.ExtensionMethod;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Ducu.UI.Responsible.Controllers
{
    public class TaskController : BaseController
    {
        #region Constructor

        private ITaskService _taskService;
        private IPlanService _planService;
        public TaskController(ITaskService taskService, IPlanService planService)
        {
            _taskService = taskService;
            _planService = planService;

        }

        #endregion

        #region Common Actions

        #region URL: /Task/Fulfil/{id}

        [NeedLogOn]
        public ActionResult Fulfil(int planId)
        {
            if (!_planService.IsCurrentUserVower(planId))
            {
                return Redirect(Prepage.GetRefuseAccess());
            }

            FulfilModel model = _taskService.GetFulfil(planId);
            return View(model);
        }

        [HttpPost]
        public ActionResult Fulfil(FulfilModel model, int planId)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            _taskService.Fulfil(model, planId);
            return RedirectToAction("Fulfil");
        }

        #endregion

        #region URL: /Task/Accept/{id}

        public ActionResult Accept(int planId, int? taskId)
        {
            AcceptModel model = new AcceptModel();

            model.AllNeedAccepts = _taskService.GetNeedAccept(planId);
            if (model.AllNeedAccepts.Count > 0)
            {
                ViewBag.StillNeedAccept = model.AllNeedAccepts.Count;
                if (!taskId.HasValue)
                {
                    taskId = model.AllNeedAccepts[0].Id;
                }
                model.Current = _taskService.Get(taskId.Value);

                if (model.AllNeedAccepts.Where(a => a.Id == taskId)
                    .SingleOrDefault() == null)
                {
                    ViewBag.OnlyComment = true;
                }
            }
            else
            {
                if (taskId.HasValue)
                {
                    model.Current = _taskService.Get(taskId.Value);
                    ViewBag.OnlyComment = true;
                }
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Accept(AcceptModel model, int planId, bool? Continue)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            _taskService.DoAccept(model);

            if (Continue.HasValue)
            {
                return RedirectToAction("Accept");
            }
            else
            {
                return RedirectToAction("HistoryMonth", "Task",
                    new
                    {
                        planId = planId,
                        year = DateTime.Now.Year,
                        month = DateTime.Now.Month
                    });
            }
        }

        #endregion

        #region URL: /Task/History/List

        public ActionResult HistoryList(int? planId, int pageIndex = 1)
        {
            int pageSize = 5;
            Pager queryPager = new Pager(pageSize, pageIndex);
            HistoryListModel model = _taskService.GetHistoryList(planId.Value, queryPager);

            int sumOfItems = _taskService.GetCount(planId.Value);

            model.Pager = new _PagerModel
            {
                PageIndex = pageIndex,
                RowSize = 5,
                SumOfPage = queryPager.GetSumOfPage(sumOfItems)
            };

            ViewBag.PlanId = planId.Value;

            return View(model);
        }

        #endregion

        #region URL: /Task/History/Month/{year}/{month}

        public ActionResult HistoryMonth(int planId, int year, int month)
        {
            HistoryMonthModel model = _taskService.GetHistoryMonth(planId, year, month);
            return View(model);
        }


        #endregion

        #region URL: /Task/NeedFulfil

        [NeedLogOn]
        public ActionResult NeedFulfil()
        {
            return RedirectToAction("Index", "Plan", new { my = "f", full = true });
        }

        #endregion

        #region URL: /Task/Supervise

        [NeedLogOn]
        public ActionResult Supervise()
        {
            return RedirectToAction("Index", "Plan",
                new { my = "s", full = true, due = false, expire = true });
        }

        #endregion

        #endregion

        #region Child Actions

        #endregion

        #region Ajax

        [NeedLogOn]
        public JsonResult _NeedFulfil()
        {
            return Json(new
            {
                items = _planService.GetNeedFulfil().Items
            }, JsonRequestBehavior.AllowGet);
        }

        [NeedLogOn]
        public JsonResult _NeedAccept()
        {
            return Json(new
            {
                items = _planService.GetNeedAccept().Items
            }, JsonRequestBehavior.AllowGet);
        }

        public PartialViewResult _HistoryItem(int taskId)
        {
            return PartialView(_taskService.GetHistoryItem(taskId));
        }

        [NeedLogOn]
        public PartialViewResult _Reply(HistoryListItemModel model)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = 400;
                return PartialView(model.Reply);
            }

            _taskService.Reply(model.TaskId,
                model.Reply.Content, model.ApplyAccept);
            return PartialView(model.Reply);
        }

        [NeedLogOn]
        public JsonResult _Leave(int planId, FulfilModel model)
        {
            if (model.TaskItem.CompleteDate == null)
            {
                throw new Exception("model.TaskItem.CompleteDate is null when invoking TaskController._Leave");
            }
            if (!_planService.CanLeave(planId, model.TaskItem.CompleteDate))
            {
                return Json(false);
            }
            else
            {
                _taskService.Leave(model.TaskItem, planId);
                return Json(true);
            }
        }

        #endregion
    }
}